﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Text;
using System.Data;
using hedefgrup.reports.renault.database;

public partial class requestheaders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition",
                 "attachment; filename=" + "2015-05-09 030120_DEV_TEAM_Ocak 2015 Fabrika Raporu" + ".xls");

        Response.BinaryWrite(File.ReadAllBytes(@"C:\Users\OOZGUL\AppData\Local\Temp\Hedef-Grup\Reports\Report_VerbatimByCode\\2015-05-09 030120_DEV_TEAM_Ocak 2015 Fabrika Raporu.xml"));

        Response.End();
        return;

        using (XmlTextWriter x = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
        {
            int sheetNumber = 0;
            x.WriteRaw("<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?>");
            x.WriteRaw("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
            x.WriteRaw("xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
            x.WriteRaw("xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            x.WriteRaw("<Styles><Style ss:ID='sText'>" +
                       "<NumberFormat ss:Format='@'/></Style>");
            x.WriteRaw("<Style ss:ID='sDate'><NumberFormat" +
                       " ss:Format='[$-409]m/d/yy\\ h:mm\\ AM/PM;@'/>");
            x.WriteRaw("</Style></Styles>");

            DataTable dt1 = db.query("SELECT TOP 10 [Key] FROM [Tree] WHERE [Path] LIKE '%QUESTIONNAIRE/%'", new object[] { });
            DataTable dt2 = db.query(" \r\n\r\nSELECT TOP 10 [Key] FROM [Tree] WHERE [Path] LIKE '%ORGANIZATION/%'", new object[] { });
            DataSet ds = new DataSet();
            dt1.TableName = "dt1";
            dt2.TableName = "dt2";
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);

            foreach (DataTable dt in ds.Tables)
            {
                sheetNumber++;
                string sheetName = !string.IsNullOrEmpty(dt.TableName) ?
                       dt.TableName : "Sheet" + sheetNumber.ToString();
                x.WriteRaw("<Worksheet ss:Name='" + sheetName + "'>");
                x.WriteRaw("<Table>");
                string[] columnTypes = new string[dt.Columns.Count];

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string colType = dt.Columns[i].DataType.ToString().ToLower();

                    if (colType.Contains("datetime"))
                    {
                        columnTypes[i] = "DateTime";
                        x.WriteRaw("<Column ss:StyleID='sDate'/>");

                    }
                    else if (colType.Contains("string"))
                    {
                        columnTypes[i] = "String";
                        x.WriteRaw("<Column ss:StyleID='sText'/>");

                    }
                    else
                    {
                        x.WriteRaw("<Column />");

                        if (colType.Contains("boolean"))
                        {
                            columnTypes[i] = "Boolean";
                        }
                        else
                        {
                            //default is some kind of number.
                            columnTypes[i] = "Number";
                        }

                    }
                }
                //column headers
                x.WriteRaw("<Row>");
                foreach (DataColumn col in dt.Columns)
                {
                    x.WriteRaw("<Cell ss:StyleID='sText'><Data ss:Type='String'>");
                    x.WriteRaw(col.ColumnName);
                    x.WriteRaw("</Data></Cell>");
                }
                x.WriteRaw("</Row>");
                //data
                bool missedNullColumn = false;
                foreach (DataRow row in dt.Rows)
                {
                    x.WriteRaw("<Row>");
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (!row.IsNull(i))
                        {
                            if (missedNullColumn)
                            {
                                int displayIndex = i + 1;
                                x.WriteRaw("<Cell ss:Index='" + displayIndex.ToString() +
                                           "'><Data ss:Type='" +
                                           columnTypes[i] + "'>");
                                missedNullColumn = false;
                            }
                            else
                            {
                                x.WriteRaw("<Cell><Data ss:Type='" +
                                           columnTypes[i] + "'>");
                            }

                            switch (columnTypes[i])
                            {
                                case "DateTime":
                                    x.WriteRaw(((DateTime)row[i]).ToString("s"));
                                    break;
                                case "Boolean":
                                    x.WriteRaw(((bool)row[i]) ? "1" : "0");
                                    break;
                                case "String":
                                    x.WriteString(row[i].ToString());
                                    break;
                                default:
                                    x.WriteString(row[i].ToString());
                                    break;
                            }

                            x.WriteRaw("</Data></Cell>");
                        }
                        else
                        {
                            missedNullColumn = true;
                        }
                    }
                    x.WriteRaw("</Row>");
                }
                x.WriteRaw("</Table></Worksheet>");
            }
            x.WriteRaw("</Workbook>");
        }
        Response.End();


        return;

        Response.Write("<pre>");
        foreach (string requestHeader in Request.Headers.AllKeys)
        {
            Response.Write(requestHeader + ": " + Request.Headers[requestHeader] + "\r\n");
        }
        Response.Write("</pre>");
    }
}